package com.content.scaffold.common.config;



import com.content.scaffold.common.constants.RedisConstant;
import com.content.scaffold.common.utils.RedisUtils;
import com.content.scaffold.common.web.RequestContext;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;



/**
 * @author xiaojl
 */
@Configuration
public class RedisConfig {


    @Resource
    RedisConstant redisConstant;

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        return new StringRedisTemplate(redisConnectionFactory);
    }

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(redisConstant.getHost());
        config.setPort(redisConstant.getPort());
        config.setDatabase(redisConstant.getDatabase());
        config.setPassword(redisConstant.getPassword());
        return new LettuceConnectionFactory(config);
    }

    @Bean("redisUtils")
    public RedisUtils<String> createRedisUtils(RedisTemplate<String, String> redisTemplate) {
        RedisUtils<String> redisUtils = new RedisUtils<>(redisTemplate);
        RequestContext.setRedisUtils(redisUtils);
        return new RedisUtils<>(redisTemplate);
    }

}
